/*
 * @Author: SJChen
 * @Date: 2021-09-16 22:09:09
 * @LastEditTime: 2022-01-10 00:32:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /iEDA/src/iTDP/cellMoveEngine/subGradient/HistogramCompression.h
 */
#pragma once

// #define DEBUG

#include <cfloat>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#include "Point.hh"
#include "contest_db.h"

namespace ieda_contest {
class ContestInstance;

class HistogramCompression
{
 public:
  HistogramCompression() = delete;
  // HistogramCompression(ContestDataManager* data_manager)
  HistogramCompression(ContestDataManager* data_manager){
  _Contest_database = data_manager->get_database();
  _timing_engine = data_manager->get_database()->get_timing_engine();
  
  buildTraverseOrder();
  initWeight();
  initHPWL();
  }
  HistogramCompression(const HistogramCompression&) = delete;
  HistogramCompression(HistogramCompression&&) = delete;
  ~HistogramCompression() = default;

  HistogramCompression& operator=(const HistogramCompression&) = delete;
  HistogramCompression& operator=(HistogramCompression&&) = delete;

  void runHistogramCompressionOpt1();

 private:
  // data
  ContestDB* _Contest_database;
  ista::TimingEngine* _timing_engine;
  std::vector<ContestInstance*> traverseList;

  int _iter{0};
  double _original_tns;  // 未优化前的timing 信息！！！
  double _original_wns;

  // params
  bool onlyConsiderLate{true};

  //调整的参数
  int maxIter{1};//调成多次试试
  double alpha = 5.0;
  double omega = 1.0;
  unsigned int total_windows_x{4};//窗口减小
  unsigned int total_windows_y{0};

  void buildTraverseOrder();
  void netWeightUpdate();
  void netWeightBalance(ContestInstance* cell);
  void netWeightDistribute(ContestInstance* inst);

  void initWeight();
  void initHPWL();

  void discreteLocalSearch();

  void localSearch(ContestInstance* cell);

  double getPinCap(ContestPin* pin);
};

}  // namespace ipl

